import java.io.File;
import java.io.StringWriter;
import java.util.ArrayList;

import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;


public class Parser implements ParserInterface {
	
	private String relatedArtisturlDebut = "http://ws.audioscrobbler.com/1.0/artist/";
	private String relatedArtisturlFin = "/similar.xml";
	private String rdfPath = "Tomcat-5.5.25-Semtag/webapps/SemanticFM/WEB-INF/data/annotations/";

	@Override
	public void getTopAlbums(String artiste) {
		applyXSLT2File(relatedArtisturlDebut + artiste + "/topalbums.xml", "topAlbums.xsl",
				artiste + "_topalbums.rdf");
	}

	@Override
	public void getTopTags(String artiste) {
		applyXSLT2File(relatedArtisturlDebut + artiste + "/toptags.xml", "topTags.xsl",
				artiste + "_toptags.rdf");
	}

	@Override
	public ArrayList<String> getRelatedArtists(String artisteRef) {
		applyXSLT2File(relatedArtisturlDebut + artisteRef + relatedArtisturlFin, "similarArtists.xsl",
				artisteRef + "_relatedArtists.rdf");
		
		String resultXsl = applyXSLT2String(relatedArtisturlDebut+artisteRef+relatedArtisturlFin, 
				"./similarArtistsPlainText.xsl");
		ArrayList<String> result = new ArrayList<String>();
		String[] array = resultXsl.split("#");
		for(String artiste:array){
			result.add(artiste);
		}
		
		
		return result;
	}
	
	/**
	 * Applique un template XSLT à un fichier XML
	 * 
	 * @param xmlurl
	 * @param xslt
	 * @param out
	 */
	private void applyXSLT(String xmlurl, String xslt, Result out){
		TransformerFactory tFactory = TransformerFactory.newInstance();

	    Transformer transformer;
		try {
			transformer = tFactory.newTransformer(new StreamSource("xsl/" + xslt));
		    transformer.transform(new StreamSource(xmlurl),out);
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Applique un template XSLT à un fichier XML et stocke le résultat dans une chaine de caratère
	 * 
	 * @param xmlurl
	 * @param xslt
	 * @return String résultat de la conversion
	 */
	private String applyXSLT2String(String xmlurl, String xslt){
		StringWriter sos = new StringWriter();
	    StreamResult out = new StreamResult(sos); 
		
		applyXSLT(xmlurl, xslt, out);
		
		return sos.toString();
	}
	
	/**
	 * Applique un template XSLT à un fichier XML et stocke le résultat dans un fichier
	 * 
	 * @param xmlurl
	 * @param xslt
	 * @param fileName
	 */
	private void applyXSLT2File(String xmlurl, String xslt, String fileName){
		File file = new File(fileName);
		Result out = new StreamResult(rdfPath + file);
		applyXSLT(xmlurl, xslt, out);
	}
	

}
